<?php


namespace app\api\model;


use app\common\model\BaseModel;
use hiredis;
use think\Db;

class StatNewModel extends BaseModel
{
    protected $name = 'stat';

    static function date_interval($date, $days) {
        $flag = $days > 0 ? 1 : -1;
        if ($flag > 0) {
            return [date('Y-m-d', strtotime($date)), date('Y-m-d', strtotime($date) + 86400 * $days)];
        } else {
            return [date('Y-m-d', strtotime($date) + 86400 * $days), date('Y-m-d', strtotime($date))];
        }
    }

    function del($city_id, $k, $f, $date) {
        $beans = [
            'city_id' => $city_id,
            'k' => $k,
            'date' => $date,
        ];
        if (!empty($f)) {
            $beans['f'] = $f;
        }

        self::destroy($beans);
    }


    function set($city_id, $k, $f, $date, $v) {
        $data = [
            'city_id' => $city_id,
            'k' => $k,
            'date' => $date,
            'v' => $v,
        ];
        if (!empty($f)) {
            $data['f'] = $f;
        }

        self::create($data,true);
    }

    function add($city_id, $k, $f, $date, $v) {
        $city_id = strval($city_id);
        $k = strval($k);
        $v = intval($v);

        $date = date('Y-m-d', strtotime($date));

        $beans = [
            'city_id'=>$city_id,
            'k'=>$k,
            'date'=>$date
        ];

        if (!empty($f)) {
            $beans['f'] = $f;
        }

        $state = $this->where($beans)->find();
        if (empty($state)){
            $this->set($city_id, $k, $f, $date, $v);
        }else{
            $val = $state->getAttr('v') + $v;
            $this->save(['v',$val]);
        }
    }



    function get($city_id, $k, $f, $date) {
        $k = strval($k);

        $date = date('Y-m-d', strtotime($date));
        $beans = [
            'k' => $k,
            'date' => $date
        ];


        if (!empty($city_id)) {
            $beans['city_id'] = $city_id;
        }

        if (!empty($f)) {
            $beans['f'] = $f;
        }

        $row = null;
        if (empty($city_id) || empty($f)) {
            $row = $this->where($beans)->group('date')->sum('v as xxx');
        }
        else
        {
            $row = $this->where($beans)->field('v as xxx')->find();
        }

        if (!empty($row)) {
            return intval($row->getAttr('xxx'));
        }
        return 0;
    }

    function gets($city_id, $k, $f = null, $sdate = null, $edate = null) {
        $k = strval($k);

        $beans = [
            'k' => $k,
        ];

        if (!empty($city_id)) {
            $beans['city_id'] = $city_id;
        }

        if (!empty($f)) {
            $beans['f'] = $f;
        }

        if (!empty($sdate)) {
            $sdate = date('Y-m-d', strtotime($sdate));
            $beans['date'] = ['>=',$sdate];
        }
        if (!empty($edate)) {
            $edate = date('Y-m-d', strtotime($edate));
            $beans['date'] = ['<=',$edate];
        }


        $rows = null;
        if (empty($city_id) || empty($f)) {
            $rows = $this->where($beans)->group('date')->sum('v as xxx');
        }
        else
        {
            $rows = $this->where($beans)->field('v as xxx')->select();
        }


        $l = [];
        if (!empty($rows)) {
            foreach ($rows as $row) {
                $l[$row->getAttr('date')] = intval($row->getAttr('xxx'));
            }
        }

        if ($edate > $sdate) {
            $days = (strtotime($edate) - strtotime($sdate)) / 86400;
            $days = floor($days);
            for ($i = 0; $i <= $days; $i++) {
                $d = date('Y-m-d', strtotime($sdate) + 86400 * $i);
                if (!isset($l[$d])) {
                    $l[$d] = 0;
                }
            }
        }
        krsort($l);
        return $l;
    }

    function sumAction($city_id, $k, $f = null, $sdate = null, $edate = null) {
        $beans = [
            'k'=> $k
        ];

        if (!empty($sdate)) {
            $sdate = date('Y-m-d', strtotime($sdate));
            $beans['date'] = ['>=',$sdate];
        }
        if (!empty($edate)) {
            $edate = date('Y-m-d', strtotime($edate));
            $beans['date'] = ['<=',$edate];
        }
        if (!empty($f)) {
            $beans['f'] = strval($f);
        }

        $row = $this->where($beans)->sum('v as sm');
        if (empty($row)){
            return 0;
        }
        else{
            return intval($row->getAttr('sm'));
        }
    }

    function repark_incr($city_id, $ok) {
        $k = self::RPORDER_COUNT;
        $f = 'OK';
        if (!$ok) {
            $f = 'NO';
        }
        $this->add($city_id, $k, $f, date('Y-m-d'), 1);
    }

    function change_battery_incr($city_id, $ok) {
        $k = self::BCORDER_COUNT;
        $f = 'OK';
        if ($ok) {
            $f = 'NO';
        }
        $this->add($city_id, $k, $f, date('Y-m-d'), 1);
    }

    function bike_count_set($city_id, $count) {
        $k = self::BIKE_COUNT;

        $d = date('Y-m-d');

        $this->del($city_id, $k, null, $d);

        $this->set($city_id, $k, null, $d, $count);
    }

    function user_incr($city_id) {
        $k = self::USER_COUNT;
        $this->add($city_id, $k, null, date('Y-m-d'), 1);
    }

    function user_auth_incr($city_id) {
        $k = self::USER_AUTH_COUNT;
        $this->add($city_id, $k, null, date('Y-m-d'), 1);
    }

    function tdcode_scaned_incr($city_id, $invalid, $date) {
        $k = self::TDCODE_SCANED;
        $f = 'OK';
        if ($invalid > 0) {
            $f = 'NO';
        }
        $date = date('Y-m-d', strtotime($date));
        $this->add($city_id, $k, $f, $date, 1);
    }

    function user_charge_incr($city_id, $amount, $date) {
        $date = date('Y-m-d', strtotime($date));
        $f = strval($amount);

        $k = self::USER_CHARGE_COUNT;
        $this->add($city_id, $k, $f, $date, 1);

        $k = self::USER_CHARGE_AMOUNT;
        $this->add($city_id, $k, $f, $date, $amount);
    }

    function user_charge_refund($city_id, $amount, $date) {
        $date = date('Y-m-d', strtotime($date));
        $f = strval($amount);

        $k = self::USER_CHARGE_REFUND_COUNT;
        $this->add($city_id, $k, $f, $date, 1);

        $k = self::USER_CHARGE_REFUND_AMOUNT;
        $this->add($city_id, $k, $f, $date, $amount);
    }

    function user_membership_incr($city_id, $days, $amount, $date) {
        $date = date('Y-m-d', strtotime($date));
        $f = strval($days);

        $k = self::USER_MEMBERSHIP_COUNT;
        $this->add($city_id, $k, $f, $date, 1);

        $k = self::USER_MEMBERSHIP_AMOUNT;
        $this->add($city_id, $k, $f, $date, $amount);
    }

    /*
     * tag : FLUSH, RCARD, PAID
     */

    function user_rider_incr($order_info) {
        $city_id = $order_info['city_id'];
        $paid_amount = $order_info['paid_amount'];
        //$gift_amount = $order_info['gift_amount'];
        $date = date('Y-m-d', strtotime($order_info['end_time']));
        $repark_fee =$order_info['fee_info']['npa_fee'] + $order_info['fee_info']['osvc_fee'];

        $k = self::USER_RIDE_ORDER_COUNT;
        $f = 'PAID';
        if ($order_info['is_flush']) {
            $f = 'FLUSH';
        }

        $this->add($city_id, $k, $f, $date, 1);

        if ($paid_amount > 0) {
            $k = self::USER_RIDE_ORDER_PAID_AMOUNT;
            $this->add($city_id, $k, null, $date, $paid_amount);
        }

//        if ($gift_amount > 0) {
//            $k = self::USER_RIDE_ORDER_GIFT_AMOUNT;
//            $this->add($city_id, $k, null, $date, $gift_amount);
//        }

        if ($repark_fee > 0) {
            $f = '?';
            if ($order_info['fee_info']['osvc_fee'] > 0) {
                $f = 'OSVC';
            } else if ($order_info['fee_info']['npa_fee'] > 0) {
                $f = 'NPA';
            }

            $k = self::USER_RIDE_ORDER_REPARK_AMOUNT;
            $this->add($city_id, $k, $f, $date, $repark_fee);
            $k = self::USER_RIDE_ORDER_REPARK_COUNT;
            $this->add($city_id, $k, $f, $date, 1);
        }
    }

    function rcard_incr($city_id, $amount, $date) {
        $k = self::USER_RCARD_COUNT;
        $f = $amount;
        $date = date('Y-m-d', strtotime($date));
        $this->add($city_id, $k, $f, $date, 1);

        $k = self::USER_RCARD_AMOUNT;
        $this->add($city_id, $k, $f, $date, $amount);
    }

    function daily_active_user_incr($city_id, $user_id) {
        $KEY = 'DAU_' . date('Ymd') . "_" . $city_id;
        $ret = hiredis::db()->zIncrBy($KEY, 1, $user_id);
        if ($ret == 1) {
            hiredis::db()->expire($KEY, 86400);
            $k = self::DAU;
            $date = date('Y-m-d');
            $this->add($city_id, $k, null, $date, 1);
        }
    }

    function city_monitor($city_id, $sdate, $edate) {
        $l = [];

        //
        //
        $l['bike_count_daily'] = $this->gets($city_id, self::BIKE_COUNT, null, $sdate, $edate);
        //用户
        $l['user_count_daily'] = $this->gets($city_id, self::USER_COUNT, null, $sdate, $edate);
        $l['user_count_total'] = $this->sumAction($city_id, self::USER_COUNT);

        //认证用户
        $l['user_auth_count_daily'] = $this->gets($city_id, self::USER_AUTH_COUNT, null, $sdate, $edate);
        $l['user_auth_count_total'] = $this->sumAction($city_id, self::USER_AUTH_COUNT);


        //扫码
        $l['tdcode_scaned_total'] = $this->sumAction($city_id, self::TDCODE_SCANED);
        $l['tdcode_scaned_daily'] = $this->gets($city_id, self::TDCODE_SCANED, null, $sdate, $edate);
        $l['tdcode_scaned_invalid_daily'] = $this->gets($city_id, self::TDCODE_SCANED, 'NO', $sdate, $edate);

        //骑行订单数
        $l['urorder_count_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_COUNT, null, $sdate, $edate);
        $l['urorder_count_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_COUNT);

        //骑行订单金额
        $l['urorder_paid_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT, null, $sdate, $edate);
        $l['urorder_paid_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT);

        //骑行订单赠送金额
        $l['urorder_gift_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT, null, $sdate, $edate);
        $l['urorder_gift_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT);

        //骑行订单-车辆调度费用
        $l['urorder_repark_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT, null, $sdate, $edate);
        $l['urorder_repark_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT);


        //用户充值数
        $l['user_charge_count_daily'] = $this->gets($city_id, self::USER_CHARGE_COUNT, null, $sdate, $edate);
        $l['user_charge_count_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT);
        $l['user_charge_count_1_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 1);
        $l['user_charge_count_100_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 100);
        $l['user_charge_count_200_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 200);
        $l['user_charge_count_500_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 500);
        $l['user_charge_count_1000_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 1000);

        //用户充值金额
        $l['user_charge_amount_daily'] = $this->gets($city_id, self::USER_CHARGE_AMOUNT, null, $sdate, $edate);
        $l['user_charge_amount_total'] = $this->sumAction($city_id, self::USER_CHARGE_AMOUNT);

        //挪车记录
        $l['rporder_count_daily'] = $this->gets($city_id, self::RPORDER_COUNT, null, $sdate, $edate);
        $l['rporder_ok_count_daily'] = $this->gets($city_id, self::RPORDER_COUNT, 'OK', $sdate, $edate);
        $l['bcorder_count_daily'] = $this->gets($city_id, self::BCORDER_COUNT, null, $sdate, $edate);
        $l['bcorder_ok_count_daily'] = $this->gets($city_id, self::BCORDER_COUNT, 'OK', $sdate, $edate);


        //用户会员
        $l['user_membership_count_daily'] = $this->gets($city_id, self::USER_MEMBERSHIP_COUNT, null, $sdate, $edate);
        $l['user_membership_count_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT);
        $l['user_membership_count_1_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 1);
        $l['user_membership_count_30_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 30);
        $l['user_membership_count_120_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 120);
        $l['user_membership_count_360_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 360);

        //用户会员金额
        $l['user_membership_amount_daily'] = $this->gets($city_id, self::USER_MEMBERSHIP_AMOUNT, null, $sdate, $edate);
        $l['user_membership_amount_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_AMOUNT);

        return $l;
    }

    function city_monitor_daily($city_id, $sdate, $edate) {
        $l = [];

        //
        //
        $l['bike_count_daily'] = $this->gets($city_id, self::BIKE_COUNT, null, $sdate, $edate);
        //用户
        $l['user_count_daily'] = $this->gets($city_id, self::USER_COUNT, null, $sdate, $edate);
        //用户
        $l['dau_daily'] = $this->gets($city_id, self::DAU, null, $sdate, $edate);

        //认证用户
        $l['user_auth_count_daily'] = $this->gets($city_id, self::USER_AUTH_COUNT, null, $sdate, $edate);


        //扫码
        $l['tdcode_scaned_daily'] = $this->gets($city_id, self::TDCODE_SCANED, null, $sdate, $edate);
        $l['tdcode_scaned_invalid_daily'] = $this->gets($city_id, self::TDCODE_SCANED, 'NO', $sdate, $edate);

        //骑行订单数
        $l['urorder_count_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_COUNT, null, $sdate, $edate);

        //骑行订单金额
        $l['urorder_paid_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT, null, $sdate, $edate);

        //骑行订单赠送金额
        $l['urorder_gift_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT, null, $sdate, $edate);

        //骑行订单-车辆调度费用
        $l['urorder_repark_amount_daily'] = $this->gets($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT, null, $sdate, $edate);


        //用户充值数
        $l['user_charge_count_daily'] = $this->gets($city_id, self::USER_CHARGE_COUNT, null, $sdate, $edate);

        //用户充值金额
        $l['user_charge_amount_daily'] = $this->gets($city_id, self::USER_CHARGE_AMOUNT, null, $sdate, $edate);

        //挪车记录
        $l['rporder_count_daily'] = $this->gets($city_id, self::RPORDER_COUNT, null, $sdate, $edate);
        $l['rporder_ok_count_daily'] = $this->gets($city_id, self::RPORDER_COUNT, 'OK', $sdate, $edate);
        $l['bcorder_count_daily'] = $this->gets($city_id, self::BCORDER_COUNT, null, $sdate, $edate);
        $l['bcorder_ok_count_daily'] = $this->gets($city_id, self::BCORDER_COUNT, 'OK', $sdate, $edate);


        //用户会员
        $l['user_membership_count_daily'] = $this->gets($city_id, self::USER_MEMBERSHIP_COUNT, null, $sdate, $edate);

        //用户会员金额
        $l['user_membership_amount_daily'] = $this->gets($city_id, self::USER_MEMBERSHIP_AMOUNT, null, $sdate, $edate);

        return $l;
    }

    function city_monitor_total($city_id) {
        $l = [];
        $l['user_count_total'] = $this->sumAction($city_id, self::USER_COUNT);

        //认证用户
        $l['user_auth_count_total'] = $this->sumAction($city_id, self::USER_AUTH_COUNT);


        //扫码
        $l['tdcode_scaned_total'] = $this->sumAction($city_id, self::TDCODE_SCANED);

        //骑行订单数
        $l['urorder_count_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_COUNT);
        $bike_count_total = $this->sumAction($city_id, self::BIKE_COUNT, null);
        $l['urorder_count_per_bike_avg'] = 0;
        if ($bike_count_total > 0) {
            $l['urorder_count_per_bike_avg'] = $l['urorder_count_total'] / $bike_count_total;
        }
        //骑行订单金额
        $l['urorder_paid_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT);

        //骑行订单赠送金额
        $l['urorder_gift_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT);

        //骑行订单-车辆调度费用
        $l['urorder_repark_amount_total'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT);


        //用户充值数
        $l['user_charge_count_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT);
        //  $l['user_charge_count_1_total'] = $this->sum($city_id, self::USER_CHARGE_COUNT, 1);
        $l['user_charge_count_1000_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 1000);
        $l['user_charge_count_2000_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 2000);
        $l['user_charge_count_5000_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 5000);
        $l['user_charge_count_10000_total'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 10000);

        //用户充值金额
        $l['user_charge_amount_total'] = $this->sumAction($city_id, self::USER_CHARGE_AMOUNT);



        //用户会员
        $l['user_membership_count_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT);
        $l['user_membership_count_1_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 1);
        $l['user_membership_count_30_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 30);
        $l['user_membership_count_120_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 120);
        $l['user_membership_count_360_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 360);

        //用户会员金额
        $l['user_membership_amount_total'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_AMOUNT);

        return $l;
    }

    function city_monitor_day($city_id, $sdate) {
        $l = [];
        //
        //
        $l['bike_count'] = $this->get($city_id, self::BIKE_COUNT, null, $sdate);
        //用户
        $l['user_count'] = $this->get($city_id, self::USER_COUNT, null, $sdate);

        //用户
        $l['dau'] = $this->get($city_id, self::DAU, null, $sdate);

        //认证用户
        $l['user_auth_count'] = $this->get($city_id, self::USER_AUTH_COUNT, null, $sdate);


        //扫码
        $l['tdcode_scaned'] = $this->get($city_id, self::TDCODE_SCANED, null, $sdate);
        $l['tdcode_scaned_invalid'] = $this->get($city_id, self::TDCODE_SCANED, 'NO', $sdate);

        //骑行订单数
        $l['urorder_count'] = $this->get($city_id, self::USER_RIDE_ORDER_COUNT, null, $sdate);

        //骑行订单金额
        $l['urorder_paid_amount'] = $this->get($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT, null, $sdate);

        //骑行订单赠送金额
        $l['urorder_gift_amount'] = $this->get($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT, null, $sdate);

        //骑行订单-车辆调度费用
        $l['urorder_repark_amount'] = $this->get($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT, null, $sdate);


        //用户充值数
        $l['user_charge_count'] = $this->get($city_id, self::USER_CHARGE_COUNT, null, $sdate);

        //用户充值金额
        $l['user_charge_amount'] = $this->get($city_id, self::USER_CHARGE_AMOUNT, null, $sdate);

        //挪车记录
        $l['rporder_count'] = $this->get($city_id, self::RPORDER_COUNT, null, $sdate);
        $l['rporder_ok_count'] = $this->get($city_id, self::RPORDER_COUNT, 'OK', $sdate);
        $l['bcorder_count'] = $this->get($city_id, self::BCORDER_COUNT, null, $sdate);
        $l['bcorder_ok_count'] = $this->get($city_id, self::BCORDER_COUNT, 'OK', $sdate);


        //用户会员
        $l['user_membership_count'] = $this->get($city_id, self::USER_MEMBERSHIP_COUNT, null, $sdate);

        //用户会员金额
        $l['user_membership_amount'] = $this->get($city_id, self::USER_MEMBERSHIP_AMOUNT, null, $sdate);


        return $l;
    }

    function city_monitor_tomonth($city_id) {
        $sdate = date('Y-m-01');
        $edate = date('Y-m-d');
        $l = [];

        $l['user_count_tomonth'] = $this->sumAction($city_id, self::USER_COUNT, null, $sdate, $edate);

        //认证用户
        $l['user_auth_count_tomonth'] = $this->sumAction($city_id, self::USER_AUTH_COUNT, null, $sdate, $edate);


        //扫码
        $l['tdcode_scaned_tomonth'] = $this->sumAction($city_id, self::TDCODE_SCANED, null, $sdate, $edate);

        //骑行订单数
        $l['urorder_count_tomonth'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_COUNT, null, $sdate, $edate);

        //骑行订单金额
        $l['urorder_paid_amount_tomonth'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT, null, $sdate, $edate);

        //骑行订单赠送金额
        $l['urorder_gift_amount_tomonth'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_GIFT_AMOUNT, null, $sdate, $edate);


        $d = intval(date('d'));
        $bike_count_total = $this->sumAction($city_id, self::BIKE_COUNT, null, $sdate, $edate);
        $l['urorder_count_per_bike_avg'] = 0;
        if ($bike_count_total > 0) {
            $l['urorder_count_per_bike_avg'] = $l['urorder_count_tomonth'] / $bike_count_total;
        }

        //骑行订单-车辆调度费用
        $l['urorder_repark_amount_tomonth'] = $this->sumAction($city_id, self::USER_RIDE_ORDER_REPARK_AMOUNT, null, $sdate, $edate);


        //用户充值数
        $l['user_charge_count_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, null, $sdate, $edate);
        // $l['user_charge_count_1_tomonth'] = $this->sum($city_id, self::USER_CHARGE_COUNT, 1, $sdate, $edate);
        $l['user_charge_count_1000_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 1000, $sdate, $edate);
        $l['user_charge_count_2000_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 2000, $sdate, $edate);
        $l['user_charge_count_5000_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 5000, $sdate, $edate);
        $l['user_charge_count_10000_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 10000, $sdate, $edate);
        $l['user_charge_count_20000_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_COUNT, 20000, $sdate, $edate);

        //用户充值金额
        $l['user_charge_amount_tomonth'] = $this->sumAction($city_id, self::USER_CHARGE_AMOUNT, null, $sdate, $edate);



        //用户会员
        $l['user_membership_count_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, null, $sdate, $edate);
        $l['user_membership_count_1_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 1, $sdate, $edate);
        $l['user_membership_count_30_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 30, $sdate, $edate);
        $l['user_membership_count_120_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 120, $sdate, $edate);
        $l['user_membership_count_360_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_COUNT, 360, $sdate, $edate);

        //用户会员金额
        $l['user_membership_amount_tomonth'] = $this->sumAction($city_id, self::USER_MEMBERSHIP_AMOUNT, null, $sdate, $edate);

        return $l;
    }

    function migrade_cities() {
        $cities = (new CityModel())->query_list([]);

        $cities = [
            ['id' => '330783'],
            ['id' => '421223'],
            ['id' => '330109'],
            ['id' => '350001'],
            ['id' => '410481'],
        ];

        foreach ($cities as $city) {
            $sdate = '2019-06-18';
            $edate = date('Y-m-d', strtotime('-1 day'));
            $this->migrade_city($city['id'], $sdate, $edate);
        }
    }

    function migrade_city($city_id, $start_date, $end_date) {
        global $g_app;
        $KEY = 'STAT_INIT_SDATE_POS_' . $city_id;
        $last_sdate = hiredis::db()->get($KEY);
        if (!empty($last_sdate) && $start_date < $last_sdate) {
            $start_date = date('Y-m-d', strtotime($last_sdate) + 86400);
        }

        if ($start_date >= date('Y-m-d')) {
            return;
        }

        $sdate = strtotime($start_date);
        $edate = strtotime($end_date);

        $t = floor(($edate - $sdate) / 86400) + 1;

        //用户
        $key = "DAILY_USR_GROWTH|" . $city_id;
        $kvs = hiredis::db()->hGetAll($key);
        foreach ($kvs as $date => $v) {
            if ($date == 'total') continue;
            if ($date < $start_date || $date > $end_date) continue;
            $this->set($city_id, self::USER_COUNT, null, $date, $v);
        }

        $key = "BIKE_COUNT|" . $city_id;
        $kvs = hiredis::db()->hGetAll($key);
        foreach ($kvs as $date => $v) {
            if ($date == 'total') continue;
            if ($date < $start_date || $date > $end_date) continue;
            $this->set($city_id, self::BIKE_COUNT, null, $date, $v);
        }

        /*
          //注册会员
          $st = "select count(1) as c   from tb_user where city_id = ? and idcno is not null";
          $beans = [
          $city_id,
          ];
          $result = hisql::db()->query($st, $beans);
          $rows = $result->fetchAll(Phalcon\Db::FETCH_ASSOC);
          $this->add($city_id, self::USER_AUTH_COUNT, null, '2019-06-18', $rows[0]['c']);
         */
        //扫码
        $key = "DAILY_USR_SCAN_TDCODE|" . $city_id;
        $kvs = hiredis::db()->hGetAll($key);
        foreach ($kvs as $date => $v) {
            if ($date == 'total') continue;
            if ($date < $start_date || $date > $end_date) continue;
            $this->set($city_id, self::TDCODE_SCANED, null, $date, $v);
        }


        //无效扫码
        $key = "DAILY_USR_SCAN_TDCODE_INVALID|" . $city_id;
        $kvs = hiredis::db()->hGetAll($key);
        foreach ($kvs as $date => $v) {
            if ($date == 'total') continue;
            if ($date < $start_date || $date > $end_date) continue;
            $this->set($city_id, self::TDCODE_SCANED, 'NO', $date, $v);
        }

        //骑行订单
        for ($i = 0; $i < $t; $i++) {
            $date = date('Y-m-d', $sdate + 86400 * $i);
            $beans = [
                'city_id' =>$city_id,
                'create_time' => [['>=',$date . " 00:00:00"],['<=',$date . " 23:59:59"],'and'],
                'order_state' => 100,
            ];
            $paid_count = Db::table('tb_user_ride_order')->where($beans)->count('paid_amount');
            $paid_amount = Db::table('tb_user_ride_order')->where($beans)->sum('paid_amount');
            $this->set($city_id, self::USER_RIDE_ORDER_COUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($paid_count));
            $this->set($city_id, self::USER_RIDE_ORDER_PAID_AMOUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($paid_amount));
        }

        //充值
        for ($i = 0; $i < $t; $i++) {
            $date = date('Y-m-d', $sdate + 86400 * $i);
            $beans = [
                'city_id' =>$city_id,
                'create_time' => [['>=',$date . " 00:00:00"],['<=',$date . " 23:59:59"],'and'],
                'gateway' => ['in',['WXPAY', 'ALIPAY']],
                'order_state' => 100,
            ];

            $charge_count = Db::table('tb_user_charge_order')->where($beans)->count('amount');
            $charge_amount = Db::table('tb_user_charge_order')->where($beans)->sum('amount');

            $this->set($city_id, self::USER_CHARGE_COUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($charge_count));
            $this->set($city_id, self::USER_CHARGE_AMOUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($charge_amount));
        }

        //会员卡
        for ($i = 0; $i < $t; $i++) {
            $date = date('Y-m-d', $sdate + 86400 * $i);
            $st = "select count(amount) as c, sum(amount) as s from tb_user_membership_order where city_id = ? and create_time >= ? and create_time <= ? and order_state = 100 and gateway in ('WXPAY', 'ALIPAY')";
            $beans = [
                'city_id' =>$city_id,
                'create_time' => [['>=',$date . " 00:00:00"],['<=',$date . " 23:59:59"],'and'],
                'gateway' => ['in',['WXPAY', 'ALIPAY']],
                'order_state' => 100,
            ];

            $membership_count = Db::table('tb_user_membership_order')->where($beans)->count('amount');
            $membership_amount = Db::table('tb_user_membership_order')->where($beans)->sum('amount');

            $this->set($city_id, self::USER_MEMBERSHIP_COUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($membership_count));
            $this->set($city_id, self::USER_MEMBERSHIP_AMOUNT, null, date('Y-m-d', $sdate + 86400 * $i), intval($membership_amount));
        }

        //挪车
        for ($i = 0; $i < $t; $i++) {
            $date = date('Y-m-d', $sdate + 86400 * $i);

            $beans = [
                'city_id' =>$city_id,
                'create_time' => [['>=',$date . " 00:00:00"],['<=',$date . " 23:59:59"],'and'],
                'order_state' => 100,
            ];

            $repark_order_count = Db::table('tb_bike_repark_order')->where($beans)->count('id');
            $repark_order_valid = Db::table('tb_bike_repark_order')->where($beans)->where('grade','>=',60)->count('amount');



            $all = intval($repark_order_count);
            $ok = intval($repark_order_valid);
            $no = $all - $ok;
            $this->set($city_id, self::RPORDER_COUNT, 'OK', date('Y-m-d', $sdate + 86400 * $i), $ok);
            $this->set($city_id, self::RPORDER_COUNT, 'NO', date('Y-m-d', $sdate + 86400 * $i), $no);
        }

        //换电
        for ($i = 0; $i < $t; $i++) {
            $date = date('Y-m-d', $sdate + 86400 * $i);

            $st = "select count(avalid) as c, sum(avalid) as s from (select if(increment>=60,1,0) as avalid from tb_battery_change_order where city_id = ? and create_time >= ? and create_time <= ? and order_state = 100)  t";
            $beans = [
                'city_id' =>$city_id,
                'create_time' => [['>=',$date . " 00:00:00"],['<=',$date . " 23:59:59"],'and'],
                'order_state' => 100,
            ];


            $battery_change_order_count = Db::table('tb_battery_change_order')->where($beans)->count('id');
            $battery_change_order_valid = Db::table('tb_battery_change_order')->where($beans)->where('increment','>=',60)->count('amount');



            $all = intval($battery_change_order_count);
            $ok = intval($battery_change_order_valid);
            $no = $all - $ok;
            $this->set($city_id, self::BCORDER_COUNT, 'OK', date('Y-m-d', $sdate + 86400 * $i), $ok);
            $this->set($city_id, self::BCORDER_COUNT, 'NO', date('Y-m-d', $sdate + 86400 * $i), $no);
        }
        hiredis::db()->set($KEY, $end_date);
    }

}